<?php
$email = new email();
$email->sendEmail($_POST);

class email {
	private $_Comment;
	private $_Name;
	private $_Email;

	public function __construct(){
		include('../../defines.php');
		FunctionsHelper::includeFiles(
			array(
				PUBLIC_HELPERS_DIRECTORY . 'class.phpmailer.php',
				PUBLIC_HELPERS_DIRECTORY . 'class.smtp.php'
			)
		);
	}
	
	private function _setParameters($params){
		$this->_Comment = $params["txtComment"];
		$this->_Email = $params["txtEmail"];
		$this->_Name = $params["txtName"];
	}
	
	public function sendEmail($params){
		$this->_setParameters($params);
		
		$EmailToSend = new PHPMailer(true);
		$this->_setInitialConfiguration($EmailToSend);
		$this->_setBody($EmailToSend);
		$this->_send($EmailToSend);
		print_r($EmailToSend);
		
		die(SMTPUSER);
	}
	
	private function _getBody(){
		$template = $this->_emailTemplate();
		$template = str_replace("__NAME_SURNAME__", $this->_Name, $template);
		$template = str_replace("__EMAIL__", $this->_Email, $template);
		$template = str_replace("__COMMENT__", $this->_Comment, $template);
		return $template;
	}
	
	private function _emailTemplate(){
		return '
			__NAME_SURNAME__ cuyo email es __EMAIL__ le ha enviado el siguiente mensaje: <br />
			__COMMENT__
		';
	}
	
	private function _setInitialConfiguration($email){
		$email->IsSMTP();
		$email->SMTPAuth = true;
		$email->Host = SMTPSERVER;
		$email->Helo = SMTPSERVER;
		$email->Username = SMTPUSER;
		$email->Password = SMTPPASSWORD;
		$email->SMTPDebug = true;
		$email->SMTPAuth = true;
		$email->SMTPSecure = "tls";
		$email->Port = SMTPPORT;
		$email->WordWrap = 50;
		$email->SetFrom(MAILFROM, NAMEMAILFROM);
		$email->AddAddress(MAILFROM, TONAME);
		$email->IsHTML(true);
	}

	private function _send($email){
		try{
			if (!$email->Send())
				die('No se pudo enviar el mail.');
		} catch (phpmailerException $e) {
			die ($e->errorMessage());
		} catch (Exception $e) {
			die ($e->getMessage());
		}
	}

	private function _setBody($email){
		$email->Subject = "Comentario de A Todo Tenis";
		$email->Body =
			'<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
			<html xmlns=\"http://www.w3.org/1999/xhtml\">
				<head>
					<title>A Todo Tenis</title>
				</head>
			 	<body>
			 		' . $this->_getBody() . ' 
				</body>
			</html>';
	}

}
?>